import request from '@/utils/request';
import {
  Content,
  CreateContentRequest,
  UpdateContentRequest,
  ContentListParams,
  DeleteContentParams,
  ContentDetailParams,
} from '@/types/content';
import { PaginationResponse } from '@/types/api-common';
import { Category } from '@prisma/client';

export type ContentWithCategories = Content & {
  categories: {
    category: Category;
  }[];
};

export const getContentList = (params: ContentListParams) => {
  return request.post<PaginationResponse<ContentWithCategories>>('/api/admin/contents/page', params);
};

export const createContent = (data: CreateContentRequest & { categoryIds: string[] }) => {
  return request.post<Content>('/api/admin/contents/create', data);
};

export const updateContent = (data: UpdateContentRequest & { categoryIds: string[] }) => {
  return request.post<Content>('/api/admin/contents/update', data);
};

export const deleteContent = (data: DeleteContentParams) => {
  return request.post('/api/admin/contents/delete', data);
};

export const getContentDetail = (params: ContentDetailParams) =>
  request.post<ContentWithCategories>('/api/admin/contents/detail', params);

export const syncContentIcon = (contentId: string) => {
  return request.post<{ fileId: string; url: string; path: string }>(
    '/api/admin/contents/sync-icon',
    { contentId }
  );
};